package com.algorithm.ch1.yjx.array;

import java.util.Arrays;

/**
 * @author yuanjianxin
 * @date 2018/8/14 14:31
 */
public class MoveZeroes {

    public static void main(String[] args) {
        int[] arr = {2};
        moveZeroes1(arr);

        System.out.println(Arrays.toString(arr));
    }

    public static void moveZeroes(int[] nums) {
        int index = -1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                continue;
            }
            for (int j = i - 1; j >= 0; j--) {
                if (nums[j] == 0) {
                    index = j;
                } else {
                    break;
                }
            }
            if (index != -1) {
                nums[index] = nums[i];
                nums[i] = 0;
            }
        }
    }

    //0,0,0,1,1,0
    public static void moveZeroes1(int[] nums) {
        int index = -1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0 && index >= 0) {
                nums[index] = nums[i];
                nums[i] = 0;
                index++;
                continue;
            }
            if (nums[i] == 0 && index == -1) {
                index = i;
            }
        }
    }
}
